<?php

namespace frontend\controllers;

use common\models\SysCategory;
use common\models\Tag;
use Yii;
use common\models\Article;
use common\models\ArticleSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;


/**
 * ArticleController implements the CRUD actions for Article model.
 */
class ArticleController extends Controller
{
//    public $layout = false;
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['index', 'create', 'update', 'delete'],
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                    [
                        'actions' => ['index', 'create', 'update', 'delete'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'upload'=>[
                'class' => 'common\widgets\file_upload\UploadAction',     //这里扩展地址别写错
                'config' => [
                    'imagePathFormat' => "/image/{yyyy}{mm}{dd}/{time}{rand:6}",
                ]
            ]
        ];
    }

    /**
     * Lists all Article models.
     * @return mixed
     */
    public function actionIndex()
    {
        $this->layout = false;
        $searchModel = new ArticleSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
        $model = Article::find()->andWhere(['is_deleted' => Article::ARTICLE_NOT_DELETED])->asArray()->all();
        return $this->render('archive', ['model' => $model]);
    }

    /**
     * Displays a single Article model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $this->layout = false;
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Article model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Article();
        $category = SysCategory::find()->asArray()->all();
//        var_dump($category);die;
        $model->user_id = 1;
//        $model->load(Yii::$app->request->post());
//        var_dump($model);die;
        if ($model->load(Yii::$app->request->post()) && $model->save(false)) {

            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model, 'category' => $category
            ]);
        }
    }

    /**
     * Updates an existing Article model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $category = SysCategory::find()->asArray()->all();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
                'category' => $category,
            ]);
        }
    }

    /**
     * Deletes an existing Article model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $model->is_deleted = Article::ARTICLE_IS_DELETED;
        $tags = $this->tags;
        $model->save();
        return $this->redirect(['index']);
    }

    /**
     * Finds the Article model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Article the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Article::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    /**
     * 回收站
     * @return string
     */
    public function actionRecycle()
    {
        $model = Article::find()->andWhere(['is_deleted' => Article::ARTICLE_IS_DELETED])->asArray()->all();
//        var_dump($model);die;
        return $this->render('recycle', ['model' => $model]);
    }

    /**
     * 刪除回收站中的單條文章
     * @param $id
     * @return \yii\web\Response
     */
    public function actionRecycleDelete($id)
    {
        $model = $this->findModel($id);
        $model->is_deleted = Article::ARTICLE_NOT_SHOW;
        $model->save();
        return $this->redirect('recycle');
    }

    /**
     * 回收站中的文章還原
     * @param $id
     * @return \yii\web\Response
     */
    public function actionRecycleBack($id)
    {
        $model = $this->findModel($id);
        $model->is_deleted = Article::ARTICLE_NOT_DELETED;
        $model->save();
        return $this->redirect('recycle');
    }


}
